using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Org.BouncyCastle.Bcpg.OpenPgp;
using Org.BouncyCastle.Bcpg;

using System.Collections;
using System.Threading;
namespace Pbdms.Common.SharedVariables
{
    public class PublicKeysControl
    {
        string directory= Pbdms.Common.Global.getKeyFolder();
        static ReaderWriterLock rwLock = new ReaderWriterLock();
        //get a public key
        public PgpPublicKey getPublicKey(string filename)
        {
            do
            {
                try
                {
                    rwLock.AcquireReaderLock(-1);
                }
                catch (System.ApplicationException exception)
                {
                }
            } while (!rwLock.IsReaderLockHeld);
            PgpPublicKeyRing pubRing = null;
            Stream inputStreamtemp = new MemoryStream();
            Stream inputStream = new MemoryStream();

            try
            {
                string fileIn = directory + "\\" + filename + ".asc";
                inputStreamtemp = File.OpenRead(fileIn);
                //retrieve the orignal public key ring file
                inputStream = PgpUtilities.GetDecoderStream(inputStreamtemp);
                pubRing = new PgpPublicKeyRing(inputStream);
                return pubRing.GetPublicKey();
            }
            catch (Exception ex)
            {
                
                return null;
            }
            finally
            {
                inputStream.Close();
                inputStreamtemp.Close();
                rwLock.ReleaseReaderLock();
            }

        }
        //delete key
        public void deleteKey(string keyId)
        {
            do
            {
                try
                {
                    rwLock.AcquireWriterLock(-1);
                }
                catch (System.ApplicationException exception)
                {
                }
            } while (!rwLock.IsWriterLockHeld);
            try
            {
                File.Delete(directory + "\\" + keyId + ".asc");
            }
            catch
            {
                throw new Exception("Unable to delete friend key");
            }
            finally
            {
                rwLock.ReleaseWriterLock();
            }
        }
        //add a public key
        public void AddAPublicKey(PgpPublicKey key)
        {
            do
            {
                try
                {
                    rwLock.AcquireWriterLock(-1);
                }
                catch (System.ApplicationException exception)
                {
                }
            } while (!rwLock.IsWriterLockHeld);
            Stream publickeystorage = null, publickeystoragetemp = null; 
            try
            {

                publickeystoragetemp = File.Create(directory+"\\"+key.KeyId.ToString("X")+".asc");
                publickeystorage = new ArmoredOutputStream(publickeystoragetemp);
                key.Encode(publickeystorage);
            }
            catch (Exception ex)
            {
                throw new Exception("Error occurs in adding a public key");
            }
            finally
            {
                publickeystorage.Close();
                publickeystoragetemp.Close();
                rwLock.ReleaseWriterLock();
            }
        
        }
        //copy to a directory
        public void exportKey(string key,string outFile)
        {
            do
            {
                try
                {
                    rwLock.AcquireReaderLock(-1);
                }
                catch (System.ApplicationException exception)
                {
                }
            } while (!rwLock.IsReaderLockHeld);
            try
            {
                File.Copy(directory + "\\" + key + ".asc", outFile, true);
            }
            catch
            {
                throw new Exception("Unable to export key");
            }
            finally
            {
                rwLock.ReleaseReaderLock();
            }
        }
   
    }
}
